package com.xunshangwang.pinhaohuo.widgets;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.xunshangwang.pinhaohuo.R;

/**
 * Created by xzp on 16/2/26.
 */
public class ProgressBar extends View implements ValueAnimator.AnimatorUpdateListener {
    private Paint mBackgroundPaint;
    private Paint mProgressPaint;
    private int mProcessingBackgroundColor;
    private int mFailBackgroundColor;
    private int mFullBackgroundColor;
    private int mProcessingColor;
    private int mFailColor;
    private int mFullColor;
    private boolean mFail;
    private int mProgress;
    private int mMaxProgress;
    private ValueAnimator mAnimator;
    public ProgressBar(Context context) {
        super(context);
        init(context);
    }

    public ProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    private void init(Context context){
        mBackgroundPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mBackgroundPaint.setStyle(Paint.Style.STROKE);
        int strokeWidth= (int) (context.getResources().getDisplayMetrics().density+0.5);
        mBackgroundPaint.setStrokeWidth(strokeWidth);
        mProgressPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mProgressPaint.setStyle(Paint.Style.FILL);
        mProcessingBackgroundColor= ContextCompat.getColor(context, R.color.group_progress_bg_color);
        mFailBackgroundColor= ContextCompat.getColor(context, R.color.group_progress_fail_bg_color);
        mFullBackgroundColor= ContextCompat.getColor(context, R.color.group_progress_full_bg_color);
        mProcessingColor= ContextCompat.getColor(context, R.color.group_progress_color);
        mFailColor= ContextCompat.getColor(context, R.color.group_progress_fail_color);
        mFullColor= ContextCompat.getColor(context, R.color.group_progress_full_color);
        mFail=false;
        mProgress=0;
        mMaxProgress=100;
    }

    public void invalidateData(int people,int groupNum,boolean isFail,boolean shouldAnimated){
        if(mAnimator!=null && mAnimator.isRunning()){
            mAnimator.cancel();
            mAnimator=null;
        }
        int progress= (int) (people*100f/groupNum+0.5f);
        if(progress>mMaxProgress)
            progress= mMaxProgress;
        mFail=isFail;
        if(shouldAnimated){
            mProgress=0;
            mAnimator=ValueAnimator.ofInt(0,progress);
            mAnimator.addUpdateListener(this);
            mAnimator.setDuration(progress * 10);
            mAnimator.start();
        }
        else {
            mProgress = progress;
            invalidate();
        }
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        mProgress= (int) animation.getAnimatedValue();
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(mFail){
            mBackgroundPaint.setColor(mFailBackgroundColor);
            mProgressPaint.setColor(mFailColor);
        }else {
            if(mProgress==mMaxProgress) {
                mBackgroundPaint.setColor(mFullBackgroundColor);
                mProgressPaint.setColor(mFullColor);
            }else {
                mBackgroundPaint.setColor(mProcessingBackgroundColor);
                mProgressPaint.setColor(mProcessingColor);
            }
        }

        float strokeWidth=mBackgroundPaint.getStrokeWidth();
        int radius=getHeight()/2;
        if(mProgress>0){
            float rate=mProgress*1f/mMaxProgress;
            RectF rectF=new RectF(strokeWidth,strokeWidth,(getWidth()-strokeWidth)*rate,getHeight()-strokeWidth);
            canvas.drawRoundRect(rectF,radius,radius,mProgressPaint);
        }
        RectF bgRectF=new RectF(strokeWidth,strokeWidth,getWidth()-strokeWidth,getHeight()-strokeWidth);
        canvas.drawRoundRect(bgRectF,radius,radius,mBackgroundPaint);

    }
}
